﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using PPI.Client.Logic;
using PPI.Entities.Lookup;

namespace PPI.Controls
{
    public partial class AddingHandheld : UserControl
    {
        private List<EHandheld> m_CurrentHandhelds;
        public List<EHandheld> CurrentHandhelds
        {
            get
            {
                return m_CurrentHandhelds;
            }
            set
            {
                m_CurrentHandhelds = value;
            }
        }


        public AddingHandheld()
        {
            InitializeComponent();
            this.Hide();
        }
        private void ShowHide(bool showHide)
        {
            this.Visible = showHide;
            foreach (Control c in this.Controls)
            {
                c.Visible = showHide;
            }
        }
        public new  void Hide()
        {
            ShowHide(false);
        }
        public new void Show()
        {
            ShowHide(true);
        }
        public void Clear()
        {
            txtHandheld.Clear();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string erroMessage = "";
            if (Validate(ref erroMessage))
            {
                EHandheld tmpHandheld = new EHandheld(0, txtHandheld.Text);
                LHandheld.Insert(tmpHandheld);
                if (tmpHandheld.Id > 0)
                {
                    m_CurrentHandhelds.Add(tmpHandheld);
                    OnFinishInsert(this, new EventArgs());
                }
            }
            else
            {
                MessageBox.Show(erroMessage);
            }
        }

        private bool Validate(ref string errorMessage)
        {
            StringBuilder textError = new StringBuilder();
            bool returnValue = false;
            if (!string.IsNullOrEmpty(txtHandheld.Text))
            {
                returnValue = true;
            }
            else
            {
                textError.AppendLine("Required value");
            }
            EHandheld tmpHandheld = new EHandheld(0, txtHandheld.Text);
            if (!m_CurrentHandhelds.Contains(tmpHandheld))
            {
                returnValue = true;
            }
            else
            {
                textError.AppendLine("Already Exist");
            }
            errorMessage = textError.ToString();
            return returnValue;
        }

        public event EventHandler FinishInsert;
        private void OnFinishInsert(object o, EventArgs e)
        {
            // Safely invoke an event:
            if (FinishInsert != null)
            {
                FinishInsert(o, e);
            }
        }
    }
}
